import { Component, OnInit } from '@angular/core';
import {Users} from '../../model/Users';
import {ToastService} from '../../services/toast.service';
import {AuthService} from '../../services/auth.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-regist',
  templateUrl: './regist.component.html',
  styleUrls: ['./regist.component.css']
})
export class RegistComponent implements OnInit {
  u:Users;
  repass:string;
  constructor(private toastservice:ToastService
  ,private authservice:AuthService
  ,private router:Router) {
    this.u=new Users();
  }

  ngOnInit(): void {
  }

  save(){

  if(this.u.pass!=this.repass){
    this.toastservice.showWarning('两次密码输入不一致');
  }
  else {
    console.dir(this.u);
    this.authservice.regist(this.u)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.toastservice.showinfo('注册成功，你可以用你的账号登录啦');
          this.router.navigate(['index']);
        }
        else if (data.msg=='exsistusername'){
          this.toastservice.showWarning('用户名已被使用，请更换');
        }
      else {
        this.toastservice.showError('注册错误，请重试');
        }


      })

  }

  }

  onHeadImgSelect(index:string){
    let imgpath="assets/imgs/head/h"+index+".png";
    this.u.headimg=imgpath;


  }

}
